package cate.game.play.buff.h;

import cate.game.play.buff.Buff;
import cate.game.play.buff.BuffHandler;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.AppendMove;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.Skill;
import easy.java.struct.ref.DoubleRef;

/**
 * 效果1：攻击提高40%，伤害加深提高20%，受到致命伤害时强制血量为1，为自己附加200%攻击的【护盾】   【归元】状态下不可【聚气】
 * 效果2：气功掌技能伤害的15%会转化为护盾
 * 效果3：释放完百裂脚 额外释放1次'气功掌
 */
public class 归元BH extends BuffHandler {

	private int 护盾buff;

	private int 聚气buff;

	private String 气功掌标签;

	private double 护盾系数;

	private int 气功掌护盾;

	private String 百裂脚标签;

	private int 气功掌原型;

	private int 结束添加;

	//护盾buff=？？聚气buff=？？&
	//气功掌标签=？？&护盾系数=1500&气功掌护盾=？？&
	//百裂脚标签=？？&气功掌原型=??&
	//结束添加=3

	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		护盾buff = args.getInt("护盾buff", 0);
		聚气buff = args.getInt("聚气buff", 0);
		气功掌标签 = args.get("气功掌标签");
		护盾系数 = args.getWDouble("护盾系数", 0d);
		气功掌护盾 = args.getInt("气功掌护盾", 0);
		百裂脚标签 = args.get("百裂脚标签");
		气功掌原型 = args.getInt("气功掌原型", 0);
	}

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		结束添加 = args.getInt("结束添加", 0);
	}

	private boolean avoidDead;

	@Override
	public void adjustDeadlyDamage(ActionCtx action, Fighter actor, int reason, DoubleRef doubleRef) {
		if (avoidDead) {
			return;
		}
		avoidDead = true;
		doubleRef.v = buff.owner.attr.hpCur() - 1;
		buff.owner.buff.tryAddByTid(action, 护盾buff);
	}

	@Override
	public void onInvalid(ActionCtx action, byte invalidReason) {
		if (结束添加 > 0) {
			buff.owner.buff.tryAddSome(action, 聚气buff, 结束添加);
		}
	}

	@Override
	public boolean allowAddBuff(ActionCtx action, Buff buffToAdd) {
		return buffToAdd.tid != 聚气buff;
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		if (move.skill.cfg.containTag(气功掌标签)) {
			long damage = move.getStat().getTotalSkillDamage(buff.owner.getPid());
			if (damage > 0) {
				double value = damage * 护盾系数;
				BuffParam param = new BuffParam().buffHpSet(value);
				buff.owner.buff.tryAddByTid(action, buff.owner, 气功掌护盾, param);
			}
		}
		if (move.skill.cfg.containTag(百裂脚标签) && !move.isAppend()) {
			Skill appendSkill = buff.owner.skill.getActiveSkillByProtoId(气功掌原型);
			if (appendSkill != null) {
				move.getTurn().addAppend(new AppendMove(buff.owner, appendSkill));
			}
		}
	}
}
